<?php

namespace app\api\controller;


use app\common\controller\ApiController;
use think\facade\Db;

class Task extends ApiController
{
    protected $noNeedLogin = [];
    protected $mustLogin = [];

    //最新识别技术点
    public function getLatestTechPoint()
    {
        $params = $this->request->param(); 
        $result = Db::name('jsyc_task_tech_point')->order('id','desc')->limit(10)->select();
        $result = $result ? $result->toArray() : [];
        foreach ($result as $key => $value) {
            $result[$key]['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
        }
        if ($result) {
            $this->success('获取成功',$result);
        } else {
            $this->error('获取失败');
        }
    }

    //同步大模型分析结果
    public function syncModalRst()
    {
        $params = $this->request->param();
        $taskId = $params['id'];
        $result = Db::name('jsyc_task')->where('id', $taskId)->update(['state'=>2,'complete_time'=>time()]);
        if ($result) {
            $this->success('同步成功');
        } else {
            $this->error('同步失败');
        }
    }

    // //更新任务大模型返回数据
    // public function updateModalRst()
    // {
    //     $params = $this->request->param();
    //     $taskId = $params['id'];
    //     $params['update_time'] = time();
    //     $result = Db::name('jsyc_task')->where('id', $taskId)->update($params);
    //     if ($result) {
    //         $this->success('更新成功');
    //     } else {
    //         $this->error('更新失败');
    //     }
    // }

    //更新任务
    public function updateTask()
    {
        $params = $this->request->param();
        $taskId = $params['id'];//任务ID
        $tech_point_id = $params['tech_point_id'];//技术点ID
        $experts_num = $params['experts_num'];//专家数

        $result = Db::name('jsyc_task')->where('id', $taskId)->update(['assess_state'=>1,'update_time'=>time()]);
        //更新技术点
        Db::name('jsyc_task_tech_point')->where('id', $tech_point_id)->update(['experts_num'=>$experts_num,'update_time'=>time()]);
        if ($result) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }


    //生成评估码
    public function createEvaluationCode()
    {
        $taskId = $this->request->param('id');
        //检查任务是否存在
        $taskDetail = Db::name('jsyc_task')->where(['id' => $taskId, 'delete_time' => 0])->find();
        if (!$taskDetail) {
            $this->error('任务不存在');
        }
        //用taskid 生成评估码
        $evaluationCode = $taskId . mt_rand(1000, 9999);
        //更新任务评估码
        Db::name('jsyc_task')->where(['id' => $taskId, 'delete_time' => 0])->update(['evaluation_code' => $evaluationCode]);
        $this->success('生成成功', ['evaluationCode' => $evaluationCode]);
    }


    //获取任务详情
    public function getTaskDetail()
    {
        $taskId = $this->request->param('id');
        $taskDetail = Db::name('jsyc_task')->where(['id' => $taskId, 'delete_time' => 0])->find();
        if (!$taskDetail) {
            $this->error('任务不存在');
        }
        //临时添加数据
        if ($taskDetail['state'] == 2) {
            $taskDetail['new_tech_point_count'] = mt_rand(1, 30);//随机数;
            $taskDetail['tech_topic_count'] = mt_rand(1, 10);//随机数;
            $taskDetail['pending_assess_count'] = mt_rand(1, 5);//随机数;
        }

        $taskDetail['complete_time'] = $taskDetail['complete_time'] ? date('Y-m-d H:i:s', $taskDetail['complete_time']) : '-';
        $taskDetail['create_time'] = $taskDetail['create_time'] ? date('Y-m-d H:i:s', $taskDetail['create_time']) : '-';
        $model_outputs = $taskDetail['model_outputs'] ? json_decode($taskDetail['model_outputs'], true) : [];
        //统计技术点 
        $taskDetail['model_outputs'] = $model_outputs;
       
        //技术点
        $taskDetail['tech_points'] = Db::name('jsyc_task_tech_point')->where(['task_id'=>$taskId,'delete_time'=>0])->select();
        $this->success('获取成功', $taskDetail);
    }

    //获取任务列表
    public function getTaskList()
    {
        $taskList = Db::name('jsyc_task')->where(['delete_time' => 0])->order('create_time', 'desc')->select();
        //转数组
        $taskList = !empty($taskList) ? $taskList->toArray() : [];
        foreach ($taskList as $key => $item) {
            $taskList[$key]['complete_time'] = $item['complete_time'] ? date('Y-m-d H:i:s', $item['complete_time']) : '-';
        }
        $this->success('获取成功', ['taskList' => $taskList]);
    }
}
